﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace TravelManagement
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //Taken from the example here:
        //http://www.dotnetfunda.com/articles/article114.aspx
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool authenticated = Authenticate(LoginControl.UserName, LoginControl.Password, LoginControl.RememberMeSet);

            if (authenticated)
            {

                FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, LoginControl.RememberMeSet);
                

            }
        }
        private bool Authenticate(string userName, string password, bool rememberUserName)
        {
            using (TSPGMapsDBContainer entities = new TSPGMapsDBContainer())
            {
                var user = (from userRow in entities.Users
                            where userRow.UserName == userName &&
                            userRow.Password == password
                            select userRow).SingleOrDefault();

                if (user == null)
                {
                    return false;
                }

                else
                {
                    if (user.UserName == userName && user.Password == password)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }


            }


        }

    }
}